#demo: F:\vs_code\PaddleSpeech\demos\speech_web\speech_server\src\ge2e_clone.py

#文件下载地址：
#https://paddlespeech.bj.bcebos.com/Parakeet/released_models/ge2e/ge2e_ckpt_0.3.zip
#https://paddlespeech.bj.bcebos.com/Parakeet/released_models/pwgan/pwg_aishell3_ckpt_0.5.zip
#https://paddlespeech.bj.bcebos.com/Parakeet/released_models/fastspeech2/fastspeech2_nosil_aishell3_vc1_ckpt_0.5.zip

# 一句话音色克隆
# 提取单句话的音色，进行语音合成。

import os
import shutil
import paddle
from paddlespeech.t2s.exps.voice_cloning import voice_cloning
from paddlespeech.t2s.exps.voice_cloning import parse_args


class VoiceCloneGE2E():

    def __init__(self):
        # am
        self.am = "fastspeech2_aishell3"
        self.am_config = "D:/vs_code_workspace/file/fastspeech2_nosil_aishell3_vc1_ckpt_0.5/default.yaml"
        self.am_ckpt = "D:/vs_code_workspace/file/fastspeech2_nosil_aishell3_vc1_ckpt_0.5/snapshot_iter_96400.pdz"
        self.am_stat = "D:/vs_code_workspace/file/fastspeech2_nosil_aishell3_vc1_ckpt_0.5/speech_stats.npy"
        self.phones_dict = "D:/vs_code_workspace/file/fastspeech2_nosil_aishell3_vc1_ckpt_0.5/phone_id_map.txt"
        # voc
        self.voc = "pwgan_aishell3"
        self.voc_config = "D:/vs_code_workspace/file/pwg_aishell3_ckpt_0.5/default.yaml"
        self.voc_ckpt = "D:/vs_code_workspace/file/pwg_aishell3_ckpt_0.5/snapshot_iter_1000000.pdz"
        self.voc_stat = "D:/vs_code_workspace/file/pwg_aishell3_ckpt_0.5/feats_stats.npy"
        # ge2e
        self.ge2e_params_path = "D:/vs_code_workspace/file/ge2e_ckpt_0.3/step-3000000.pdparams"

    def vc(self, text, input_wav, out_wav):

        # input wav 需要形成临时单独文件夹
        _, full_file_name = os.path.split(input_wav)
        # ref_audio_dir = os.path.realpath("D:/vs_code_workspace/file/tmp/ge2e")
        ref_audio_dir = os.path.realpath("D:/vs_code_workspace/file/tmp/ge2e")
        #if os.path.exists(ref_audio_dir):
        #    shutil.rmtree(ref_audio_dir)

        #os.makedirs(ref_audio_dir, exist_ok=True)
        # shutil.copy(input_wav, ref_audio_dir)

        output_dir = os.path.dirname(out_wav)

        ngpu =  0 if paddle.device.get_device() == "cpu" else 1

        args = parse_args()
        args.am = self.am
        args.am_config = self.am_config
        args.am_ckpt = self.am_ckpt
        args.am_stat=self.am_stat
        args.voc=self.voc
        args.voc_config=self.voc_config
        args.voc_ckpt=self.voc_ckpt
        args.voc_stat=self.voc_stat
        args.ge2e_params_path=self.ge2e_params_path
        args.text=text
        args.input_dir=ref_audio_dir
        args.output_dir=output_dir
        args.phones_dict=self.phones_dict
        args.ngpu=ngpu
        
        voice_cloning(
            args
        )
        output_name = os.path.join(output_dir, full_file_name)
        if os.path.exists(output_name):
            return output_name



vc_model = VoiceCloneGE2E()
vc_model.__init__()
wavPath = vc_model.vc(text="欢迎所有直播间的宝贝们、家人们，欢迎你们来到我们家的直播间啊",
                       input_wav="D:/vs_code_workspace/file/test-video/tag01.wav",
                       out_wav="D:/vs_code_workspace/output/")
res = {"wavPath": wavPath}
print(res)



